
#import "RootViewController.h"
#import "RootView.h"
#import "MusicModel.h"
#import "MusicViewController.h"
#import "MusicSharedData.h"
typedef void(^Block)();
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)RootView *rootView;
//@property(nonatomic, strong)NSMutableArray *dataArray;
@end

@implementation RootViewController


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self initalData];
    }
    return self;
}

#pragma mark 初始化工作
-(void)initalData
{
//  NSMutableArray *array = [NSMutableArray array];
    MusicModel *music1 = [[MusicModel alloc] initWithMusicName:@"情非得已" withMusicType:@"mp3"];
    MusicModel *music2 = [[MusicModel alloc] initWithMusicName:@"梁静茹-偶阵雨" withMusicType:@"mp3"];
    MusicModel *music3 = [[MusicModel alloc] initWithMusicName:@"林俊杰-背对背拥抱" withMusicType:@"mp3"];

    [[MusicSharedData sharedInstance].musicArray addObject:music1];
    [[MusicSharedData sharedInstance].musicArray addObject:music2];
    [[MusicSharedData sharedInstance].musicArray addObject:music3];

}


-(void)loadView
{
    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rootView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音乐列表";
    self.rootView.mainTableView.delegate = self;
    self.rootView.mainTableView.dataSource = self;
}


#pragma mark datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [MusicSharedData sharedInstance].musicArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_id = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }

    cell.textLabel.text = [[MusicSharedData sharedInstance].musicArray[indexPath.row] musicName];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicViewController *musicVC = [[MusicViewController alloc] init];
    
    musicVC.playIndex = indexPath.row;
    [self.navigationController pushViewController:musicVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded] && self.view.window == nil) {
        self.view = nil;
    }

}



@end
